const webdriver = require('selenium-webdriver');
const Promise = require('bluebird');

const gm = Promise.promisifyAll(require('gm'));

const fs = Promise.promisifyAll(require('fs'));

const By = webdriver.By;


let driver = new webdriver.Builder()
  .usingServer('http://localhost:9515')
  .withCapabilities({
    chromeOptions: {
      binary: "/Applications/MakeBlock/mblock.app/Contents/MacOS/mblock"
    }
  })
  .forBrowser('electron')
  .build();



let init = false;
let startShot = false;
let endShot = false;

setInterval(async () => {

  if (!init) {
    driver.executeScript(`document.getElementsByClassName('blocklyMainBackground')[0].style.fill = 'white'`);
    init = true;
  }

  driver.findElement(By.className('select_menu')).getCssValue('display').then(async trigger => {
    if (trigger === 'block' && !startShot && !endShot) {
      console.log("take screenshot");
      startShot = true;
      await takeShot();
    }
    if (endShot) {
      await driver.findElement(By.className('select_value')).click();
      startShot = false;
      endShot = false;
    }
  });

}, 300);



async function takeShot() {

  let rect = await driver.findElement(By.className("blocklyBlockCanvas")).getRect();
  let image = await driver.takeScreenshot();
  let now = new Date().getTime();

  await fs.writeFileAsync(__dirname + `/images/${now}_zh.png`, image, 'base64')

  gm(__dirname + `/images/${now}_zh.png`)
    .crop(rect.width * 2 + 20, rect.height * 2 + 20, rect.x * 2 - 10, rect.y * 2 - 10)
    .write(__dirname + `/images/${now}_zh.png`, async err => {
      if (err) {
        console.log('err--->' + err)
      };
      await driver.executeScript('Mscratch.instance.setLang("en")');
      let rect2 = await driver.findElement(By.className("blocklyBlockCanvas")).getRect();
      let image2 = await driver.takeScreenshot();
      await fs.writeFileAsync(__dirname + `/images/${now}_en.png`, image2, 'base64')
      gm(__dirname + `/images/${now}_en.png`)
        .crop(rect2.width * 2 + 20, rect2.height * 2 + 20, rect2.x * 2 - 10, rect2.y * 2 - 10)
        .write(__dirname + `/images/${now}_en.png`, async err => {
          if (err) {
            console.log('err--->' + err)
          };
          await driver.executeScript('Mscratch.instance.setLang("zh-hant")');
          let rect3 = await driver.findElement(By.className("blocklyBlockCanvas")).getRect();
          let image3 = await driver.takeScreenshot();
          await fs.writeFileAsync(__dirname + `/images/${now}_zh-hant.png`, image3, 'base64')
          gm(__dirname + `/images/${now}_zh-hant.png`)
            .crop(rect3.width * 2 + 20, rect3.height * 2 + 20, rect3.x * 2 - 10, rect3.y * 2 - 10)
            .write(__dirname + `/images/${now}_zh-hant.png`, async err => {
              if (err) {
                console.log('err--->' + err)
              };
              await driver.executeScript('Mscratch.instance.setLang("zh")');
              console.log('ok');
              endShot = true;
            });
        });
    });
};
